package com.airhockey;

import java.net.InetAddress;
import java.net.UnknownHostException;

import com.airhockey.R;

import Jeu.Jeu;
import Partage.PartageClient;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.app.Activity;
import android.content.SharedPreferences;

/**
 * Game activity , manage the game with the help of a Thread and is composed
 * of a graphicView and a Partage object
 * 
 * @author Maxime
 */
public class GameActivity extends Activity {

	public enum ObjectType
	{
		all,
		pusher,
		pusher2,
		palet,
	}
	
	//thread de gestiond de la partie
	ThreadGestionPartieClient gestionPartie=null;
	//vue
	GraphicView graphicView;
	//partage
	public PartageClient partage;
	



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_game);    

        RelativeLayout layout = (RelativeLayout) findViewById(R.id.relativeLayout);
        graphicView = new GraphicView(this);
        layout.addView(graphicView,0);
        
        HideAll();
        
        TextView message = (TextView) findViewById(R.id.textViewMessage);
        message.setVisibility(View.INVISIBLE);
        
        //lancement du thread gestion de partie
        InitThread();
      
    }
    
    /**
     * Hide the score and all the objects
     */
    public void HideAll()
    {
       
        LinearLayout score = (LinearLayout) findViewById(R.id.LinearLayoutScore);
        score.setVisibility(View.INVISIBLE);
        
        graphicView.graphicObjectsvisible=false;
    }
    
    /**
     * Show the score and all the objects 
     */
    public void ShowAll()
    {
    	  LinearLayout score = (LinearLayout) findViewById(R.id.LinearLayoutScore);
          score.setVisibility(View.VISIBLE);
          
          graphicView.graphicObjectsvisible=true;
    }
    
    /**
     * Update the score using the 'jeu' object
     */
    public void SetScore()
    {
    	TextView score = (TextView) findViewById(R.id.textViewScore);
    	TextView scoreAdv = (TextView) findViewById(R.id.textViewScoreAdversory);
    	
    	score.setText(String.valueOf(partage.jeu.points[0]));
    	scoreAdv.setText(String.valueOf(partage.jeu.points[1]));
    }
    
    /**
     * Display a message
     */
    public void ShowMessage(String s)
    {
    	TextView message = (TextView) findViewById(R.id.textViewMessage);
    	message.setVisibility(View.VISIBLE);
    	
    	message.setText(s);

    	Animation anim = AnimationUtils.loadAnimation(getApplicationContext(),
    			R.anim.textinout); 

    	message.startAnimation(anim);
    	
    	message.setVisibility(View.INVISIBLE);
    }

    @Override
    public void onDestroy()
    {

        super.onDestroy();
        
        /* 
         * avant de quitter , on indique que l'on ne continue plus le jeu
         * ainsi , le thread gestion de partie provoque une deconnexion propre
         */
        
        synchronized (partage.jeu) {
        	 partage.continueJeu=false;
             partage.jeu.notifyAll();
		}

    }
    
    /**
     * Lauching of the thread that will handle the game
     */
    public void InitThread()
    {
    	SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
		
		try 
    	{
    		partage = new PartageClient();
    		gestionPartie = new ThreadGestionPartieClient(partage, 
    				InetAddress.getByName(sharedPreferences.getString(AppData.ipServerKey,AppData.ipServerDefault)), 
    				sharedPreferences.getInt(AppData.portKey,AppData.portDefault), 
    				sharedPreferences.getString(AppData.pseudoKey,AppData.pseudoDefault),
    				this);
    		
    		gestionPartie.start();
    		
    	} 
		catch (InterruptedException e1) 
    	{
    		e1.printStackTrace();
    	} 
		catch (UnknownHostException e) 
    	{
			e.printStackTrace();
		}
    }
    
    /**
     * Update of the names
     */
    public void setNames()
    {
    	TextView you = (TextView) findViewById(R.id.textViewYou);
    	you.setText(gestionPartie.partage.jeu.nomJoueurs[0]);
    	TextView adv = (TextView) findViewById(R.id.textViewAdversory);
    	adv.setText(gestionPartie.partage.jeu.nomJoueurs[1]);
    }
    
    /**
     * Sending of the finger position
     * @param x
     * @param y
     */
    public void SendPusherPosition(int x,int y)
    {
    	int [] p = {x, y};
    	
    	int [] serverPositionPusher = gestionPartie.getPosition(p,
    			graphicView.width,
    			graphicView.height, 
    			Jeu.taillePlateau[0], 
    			Jeu.taillePlateau[1]);
    	
    	gestionPartie.envoiPositionPusher(serverPositionPusher);
    }

    /**
     * Update of the positions of the objects
     * @param objectType
     */
    public void setPosition(ObjectType objectType)
    {
    	if(objectType == ObjectType.pusher || objectType == ObjectType.all)
    	{
	    	int [] clientPositionPusher = gestionPartie.getPosition(partage.jeu.posJoueurs[0],
	    			Jeu.taillePlateau[0],
	    			Jeu.taillePlateau[1], 
	    			graphicView.width, 
	    			graphicView.height);
	    	
		    graphicView.pusher.setPosition(clientPositionPusher[0],clientPositionPusher[1]);
    	}
    	
    	if(objectType == ObjectType.pusher2 || objectType == ObjectType.all)
    	{
		    int [] clientPositionPusher2 = gestionPartie.getPosition(partage.jeu.posJoueurs[1],
	    			Jeu.taillePlateau[0],
	    			Jeu.taillePlateau[1], 
	    			graphicView.width, 
	    			graphicView.height);
		    
		    graphicView.pusher2.setPosition(clientPositionPusher2[0],clientPositionPusher2[1]);
    	}
    	
    	if(objectType == ObjectType.palet || objectType == ObjectType.all)
    	{
		    int [] clientPositionPalet = gestionPartie.getPosition(partage.jeu.posPalet,
	    			Jeu.taillePlateau[0],
	    			Jeu.taillePlateau[1], 
	    			graphicView.width, 
	    			graphicView.height);
		    
		    graphicView.palet.setPosition(clientPositionPalet[0],clientPositionPalet[1]);
    	}
    }
}

